package day9;

class Sell2 extends Thread {

    // 票数
    static int ticket = 10;

    public Sell2 (String name) {
        super(name);
    }

    // 对象
    // static Object obj = new Object();
    static Integer i = new Integer(3);

    @Override
    public void run() {
        while (true) {

            synchronized (Sell2.class) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(this.getName() + "正在售票:" + (ticket--) + "票");
                } else {
                    break;
                }
            }
        }
    }
}

public class 同步Demo3 {
    public static void main(String[] args) {
        Sell2 s1 = new Sell2("南昌西站");
        Sell2 s2 = new Sell2("南昌站");
        Sell2 s3 = new Sell2("南昌南站");

        s1.start();
        s2.start();
        s3.start();
    }
}
